package com.zsrt.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsrt.Entity.UserEntity;
import com.zsrt.service.UserServiceImpl;

public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String userCellphone = req.getParameter("userCellphone");
		String password = req.getParameter("password");
		UserEntity user = new UserServiceImpl().query(userCellphone);
		System.out.println(user.getUserCellphone());
		res.setContentType("text/html;charset=utf-8");
		PrintWriter out = res.getWriter();
		if (user.getUserCellphone()==null) {
			UserEntity user1 = new UserEntity();
			user1.setPassword(password);
			user1.setUserCellphone(userCellphone);
			boolean flag = new UserServiceImpl().add(user1);
			if (flag) {
				out.write("注册成功！");
				out.flush();
				out.close();
			} else {
				out.write("注册失败");
				out.flush();
				out.close();
			}
		}else{
			out.write("手机号已被注册");
			out.flush();
			out.close();
		}
	}

}
